﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib.GSMModem;
using WFNetLib;
using System.IO.Ports;
using System.Threading;
using System.Diagnostics;

namespace ATReaderTest
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public event strEventHandler strEvent;
        Form3 f;
        List<Thread> ReaderThreadList;
        private void Form2_Load(object sender, EventArgs e)
        {
            ReaderThread.AddLogEvent += new WFNetLib.strEventHandler(ReaderThread_AddLogEvent);
            ReaderThreadList = new List<Thread>();
            if (Properties.Settings.Default.Coms != "")
            {
                string[] coms = Properties.Settings.Default.Coms.Split(new string[]{";"},StringSplitOptions.RemoveEmptyEntries);
                listBox1.Items.Clear();
                listBox1.Items.AddRange(coms);
            }
        }
        void OnStrEvent(strEventArgs e)
        {
            if (strEvent != null)
                strEvent(this, e);
        }
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
//             char rx;
//             while (serialPort1.BytesToRead != 0)
//             {
//                 int irx = serialPort1.ReadByte();
//                 if (irx == -1)
//                     break;
//                 rx = (char)irx;
//                 this.Invoke((EventHandler)(delegate
//                     {
//                         textBox2.Text += rx;
//                     }));
//             }
        }
        Thread ReaderThreadIns;        
        private void button1_Click(object sender, EventArgs e)
        {
            
//             ReaderThreadIns = new Thread(new ThreadStart(sjie));
//             ReaderThreadIns.Name = "com";
//             ReaderThreadIns.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SerialPort com = new SerialPort("COM8", 115200);
            com.Handshake = Handshake.RequestToSend;
            com.DtrEnable = true;            
            //com.RtsEnable = true;
            ReaderThread read = new ReaderThread(com);
            
            read.ReaderThreadStart();
        }

        void ReaderThread_AddLogEvent(object sender, strEventArgs e)
        {
            this.Invoke((EventHandler)(delegate
                     {
                         ReaderThread read = (ReaderThread)sender;
                         textBox2.AppendText(read.ReaderThreadIns.Name+":"+e.strArg+"\r\n");
                         int x=e.strArg.IndexOf("<");
                         if (x != -1)
                         {
                             foreach (Thread t in ReaderThreadList)
                             {
                                 if (t.Name == read.ReaderThreadIns.Name)
                                 {
                                     //while (t.IsAlive) ;
                                     ReaderThreadList.Remove(t);
                                     for (int i = 0; i < listBox1.Items.Count; i++)
                                     {
                                         string com = listBox1.Items[i].ToString();
                                         if (com.IndexOf('-') != -1)
                                             com=com.Substring(0, com.IndexOf('-'));
                                         if (com == read.PortName)
                                         {
                                             listBox1.Items[i] = com+"---"+e.strArg.Substring(x+1,e.strArg.IndexOf(">",x+1)-(x+1));
                                             break;
                                         }
                                     }                                       
                                     break;
                                 }
                             }
                         }
                     }));
        }
//         SerialPort com1;
//         void sjie()
//         {            
//                             
//                 com1.Open();
//                
//                       com1.ReadLine();
//                       while (true) ;
//                     
// 
//         }

        private void button3_Click(object sender, EventArgs e)
        {
            //com1.Close();
            //com1.ReadLine();
//             if (ReaderThreadIns.IsAlive)
//             {
//                 ReaderThreadIns.Abort();
//                 while (ReaderThreadIns.IsAlive) ;
//                     //com1.Open();
//                 //Debug.WriteLine("2");
//                 
//             }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            bool bAdd = false;
            for (int i = 0; i < listBox1.Items.Count;i++ )
            {
                bAdd = true;
                string com = listBox1.Items[i].ToString();
                if (com.IndexOf('-') != -1)
                    com=com.Substring(0, com.IndexOf('-'));
                listBox1.Items[i] = com + "---开始";
                foreach (Thread t in ReaderThreadList)
                {
                    if (t.Name.IndexOf(com) != -1)
                    {
                        if (t.IsAlive)
                        {
                            bAdd = false;
                            break;
                        }
                    }
                }
                if (bAdd)
                {
                    SerialPort Com = new SerialPort(com, 115200);
                    Com.Handshake = Handshake.RequestToSend;
                    Com.DtrEnable = true;
                    //com.RtsEnable = true;
                    ReaderThread read = new ReaderThread(Com);
                    read.ReaderThreadStart();
                    ReaderThreadList.Add(read.ReaderThreadIns);                    
                }
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Form4 f = new Form4();
            if(f.ShowDialog()==DialogResult.OK)
            {
                listBox1.Items.Clear();
                listBox1.Items.AddRange(f.AddrList.ToArray());
                Properties.Settings.Default.Coms = "";
                foreach (string c in f.AddrList)
                {
                    Properties.Settings.Default.Coms += c + ";";
                }
                Properties.Settings.Default.Save();
            }
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
                return;
            string com=listBox1.Items[listBox1.SelectedIndex].ToString();
            if (com.IndexOf('-') != -1)
                com=com.Substring(0, com.IndexOf('-'));
            foreach (Thread t in ReaderThreadList)
            {
                if (t.Name.IndexOf(com) != -1)
                {
                    if(t.IsAlive)
                        return;
                }
            }
            SerialPort Com = new SerialPort(com, 115200);
            Com.Handshake = Handshake.RequestToSend;
            Com.DtrEnable = true;
            //com.RtsEnable = true;
            ReaderThread read = new ReaderThread(Com);
            read.ReaderThreadStart();
            ReaderThreadList.Add(read.ReaderThreadIns);
            listBox1.Items[listBox1.SelectedIndex] = com + "---开始";
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
//             if (listBox1.SelectedIndex == -1)
//                 return;
//             string com = listBox1.Items[listBox1.SelectedIndex].ToString();
//             if (com.IndexOf('-') == -1)
//                 return;
//             com.Substring(0, com.IndexOf('-'));
//             listBox1.Items[listBox1.SelectedIndex] = com;
//             foreach (Thread t in ReaderThreadList)
//             {
//                 if (t.Name.IndexOf(com) != -1)
//                 {
//                     if (!t.IsAlive)
//                         return;
//                     SerialPort c = new SerialPort(com);
//                     c.Close();
//                     t.Abort();
//                 }
//             }            
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
